﻿using System.Linq;
using System.Web.Mvc;
using Olemas.Models;
namespace Olemas.Controllers
{
    public class AutoCompleteController : Controller
    {
        private readonly OlemasDatabaseEntities _db = new OlemasDatabaseEntities();
        //
        // GET: /AutoComplete/

        /// <summary>
        /// Populates the autocomplete textbox with matching data.
        /// </summary>
        /// <param name="term">The search string.</param>
        public ActionResult Find(string term)
        {
            var bookResults = (from book in _db.Books
                               where book.Name.Contains(term) || book.Author.Contains(term)
                               select book.Name);
            var conspectResults = (from conspect in _db.Conspects
                                   where conspect.Name.Contains(term) || conspect.Course.Name.Contains(term)
                                   select conspect.Name);
            var courseResults = (from course in _db.Courses
                                 where course.Name.Contains(term) || course.Faculty.Name.Contains(term)
                                 select course.Name);
            var testResults = (from test in _db.Tests
                               where test.Name.Contains(term)
                               select test.Name);
            var results = conspectResults.Union(bookResults).Union(courseResults).Union(testResults).Take(10).Distinct();

            return Json(results, JsonRequestBehavior.AllowGet);

        }

    }
}
